package com.ruoyi.store.controller;

import com.ruoyi.common.core.flashpay.utils.IdWorker;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.store.domain.TbStore;
import com.ruoyi.store.service.ITbStoreService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 门店Controller
 *
 * @author ruoyi
 * @date 2023-04-24
 */
@RestController
@RequestMapping("/store")
public class TbStoreController extends BaseController {
    @Autowired
    private ITbStoreService tbStoreService;
    @Autowired
    private IdWorker idWorker;

    /**
     * 查询门店列表
     */
    @RequiresPermissions("store:store:list")
    @GetMapping("/list")
    public TableDataInfo list(TbStore tbStore) {
        startPage();
        List<TbStore> list = tbStoreService.selectTbStoreList(tbStore);
        return getDataTable(list);
    }

    /**
     * 导出门店列表
     */
    @RequiresPermissions("store:store:export")
    @Log(title = "门店", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TbStore tbStore) {
        List<TbStore> list = tbStoreService.selectTbStoreList(tbStore);
        ExcelUtil<TbStore> util = new ExcelUtil<TbStore>(TbStore.class);
        util.exportExcel(response, list, "门店数据");
    }

    /**
     * 获取门店详细信息
     */
    @RequiresPermissions("store:store:query")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId) {
        return success(tbStoreService.selectTbStoreByStoreId(storeId));
    }

    /**
     * 新增门店
     */
    @RequiresPermissions("store:store:add")
    @Log(title = "门店", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TbStore tbStore) {
        tbStore.setStoreId(idWorker.nextId());
        tbStore.setStatus(1);
        return toAjax(tbStoreService.insertTbStore(tbStore));
    }

    /**
     * 修改门店
     */
    @RequiresPermissions("store:store:edit")
    @Log(title = "门店", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TbStore tbStore) {
        return toAjax(tbStoreService.updateTbStore(tbStore));
    }

    /**
     * 删除门店
     */
    @RequiresPermissions("store:store:remove")
    @Log(title = "门店", businessType = BusinessType.DELETE)
    @DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds) {
        return toAjax(tbStoreService.deleteTbStoreByStoreIds(storeIds));
    }

//    @GetMapping("/getByLessid/{id}")
//    public AjaxResult getByLesseeid(@PathVariable Long id){
//        return AjaxResult.success();
//    }
}
